package com.inhim.pjhim.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.app.BaseActivity;
import com.inhim.pjhim.fragment.AlllCateFragment;

import java.util.ArrayList;

public class AlllCateActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private ArrayList<Fragment> fs;
    private RadioButton radio1,radio2,radio3;
    private Context mContext;
    private int position;
    private ImageView iv_back;
    private TextView tvCourse;
    @Override
    public Object offerLayout() {
        return R.layout.activity_alll_cate;
    }

    @Override
    public void onBindView() {
        initView();
        //getReaderStyle();
        position=getIntent().getIntExtra("position",0);
        setFragment();
    }

    private void setFragment() {
        Bundle bundle1=new Bundle();
        bundle1.putSerializable("Type","2");
        bundle1.putSerializable("Title","视频");
        Bundle bundle2=new Bundle();
        bundle2.putSerializable("Type","3");
        bundle2.putSerializable("Title","聆听");
        Bundle bundle3=new Bundle();
        bundle3.putSerializable("Type","1");
        bundle3.putSerializable("Title","阅读");
        AlllCateFragment readingTwoFragment1=new AlllCateFragment();
        AlllCateFragment readingTwoFragment2=new AlllCateFragment();
        AlllCateFragment readingTwoFragment3=new AlllCateFragment();
        readingTwoFragment1.setArguments(bundle1);
        readingTwoFragment2.setArguments(bundle2);
         readingTwoFragment3.setArguments(bundle3);
        fs=new ArrayList<>();
        fs.add(readingTwoFragment1);
        fs.add(readingTwoFragment2);
        fs.add(readingTwoFragment3);
        FragmentManager fm=getSupportFragmentManager();
        MyPagerAdapter adapter=new MyPagerAdapter(fm);
        viewPager.setAdapter(adapter);
        viewPager .setOffscreenPageLimit(3);
        setListener();
        viewPager.setCurrentItem(position);
    }

    private void setListener() {
        // TODO Auto-generated method stub
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radio1.setChecked(true);
                        break;
                    case 1:
                        radio2.setChecked(true);
                        break;
                    case 2:
                        radio3.setChecked(true);
                        break;
                }

            }
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void initView() {
        viewPager=findViewById(R.id.viewPager);
        radio1=findViewById(R.id.radio1);
        radio2=findViewById(R.id.radio2);
        radio3=findViewById(R.id.radio3);
        radio1.setOnClickListener(this);
        radio2.setOnClickListener(this);
        radio3.setOnClickListener(this);
        iv_back=findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        tvCourse=findViewById(R.id.tvCourse);
        tvCourse.setText("全部分类");
    }

    /**
     * 自定义适配器  FragmentPagerAdapter
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            return fs.get(arg0);
        }

        @Override
        public int getCount() {
            return fs.size();
        }
    }

    @Override
    public void destory() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radio1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.radio2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.radio3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }
}
